package com.project.controller.user;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.controller.BaseController;
import com.project.entity.activity.ActivityLog;
import com.project.entity.activity.ActivityUser;
import com.project.entity.enumcommon.AuditStatusEnum;
import com.project.entity.enumcommon.UserTypeEnum;
import com.project.entity.sms.SmsMessageVo;
import com.project.entity.user.Member;
import com.project.entity.user.User;
import com.project.service.activitylog.ActivityLogService;
import com.project.service.sms.SmsService;
import com.project.service.user.UserService;
import com.project.util.CurrentSessionUtil;
import com.project.util.HttpRequest;

/**
 * 用于获取：首页> 用户管理 > 入会审核
 * @author guo.junhui
 *
 */
@Controller
@RequestMapping("/user")
public class MemberController extends BaseController<Member>{
	
	private final static Logger logger = LoggerFactory.getLogger(MemberController.class);
	
	@Autowired
	private ActivityLogService activityLogService;
	@Autowired
	private SmsService smsService;
	
	@Resource(name="bUserService")
	UserService userService;
	
	@Override
	protected Class<Member> getClazz() {
		return Member.class;
	}
	
	//用户管理-入会审核
	@RequestMapping("audit")
	public String list(Model model,@ModelAttribute Member member) {
		model.addAttribute("entity", member);
		model.addAttribute("list", service.query(member));
		return "/user/audit";
	}
	
	//用户管理-入会审核详情页
	@RequestMapping("auditDetail")
	public String auditDetail(Model model,@ModelAttribute Member member) {
		Member retMember = service.queryEntity(member);
		model.addAttribute("retMember",retMember);
		
		User user = new User();
		user.setId(retMember.getUserId());
		User retUser = userService.queryEntity(user);
		model.addAttribute("retUser",retUser);
		
		ActivityLog activityLog = new ActivityLog();
		ActivityUser activityUser = new ActivityUser();
		activityUser.setUserId(retMember.getUserId());
		activityLog.setActivityUser(activityUser);
		List<ActivityLog> activityLogs = activityLogService.query(activityLog);
		model.addAttribute("activityLogs",activityLogs);
		return "/user/auditDetail";
	}
	
	//保存审核结果
	@RequestMapping("updateAudit")
	@ResponseBody
	public void updateAudit(@RequestParam("id") String id, 
			@RequestParam("auditDescription") String auditDescription,
			@RequestParam("auditStatus") String auditStatus) {
		
		Member tempMember = new Member();
		tempMember.setId(Long.parseLong(id));
		Member retMember = service.queryEntity(tempMember);
		retMember.setAuditDescription(auditDescription);
		retMember.setAuditStatus(AuditStatusEnum.getInstance(auditStatus));
		retMember.setAuditTime(new Date());
		retMember.setAuditUser(CurrentSessionUtil.getCurrentUser().getUserCode());
		
		User user = new User();
		user.setId(retMember.getUserId());
		User retUser = userService.queryEntity(user);
		
		String mobile = retMember.getMobile();
		String username = retMember.getName();
		String result = "审核" + retMember.getAuditStatus().getValue();
		String description = retMember.getAuditDescription();
		
		if(StringUtils.isEmpty(description)){
			description = " ";
		}
		
		if("通过".equals(auditStatus)){
			retUser.setUserType(UserTypeEnum.H);
		}else{
			retUser.setUserType(UserTypeEnum.Z);
		}
		
		service.update(retMember);
		userService.update(retUser);
		sendAuditSMS(mobile, username, result, description);
	}
	
	/**
	 * 发送入会审核结果短信
	 * @param mobile
	 * @param username
	 * @param reason
	 * @param other
	 */
	@Async
	private void sendAuditSMS(String mobile, String username, String result, String description){
		//尊敬的{1}先生/女士，您提交的私董会入会申请已审核，结果为：{6}。相关说明：{8}。
		SmsMessageVo smsMessageVo = new SmsMessageVo();
		smsMessageVo.setTaget(mobile);
		smsMessageVo.setSendPoint("SMS_SEND_MEMBER_AUDIT");
		smsMessageVo.addValue("username", username);
		smsMessageVo.addValue("reason", result);
		smsMessageVo.addValue("other", description);
		smsService.sendSms(smsMessageVo);
		
		/*String p = "commAppVo={\"appId\":\"20160901qinqu\",\"appSecret\":\"28e6ea65fa3b2e068ea5cbd9bb5edd6e\"}"
		        + "&smsMessageVo={\"targets\": [\"" + mobile + "\"], \"sendPoint\": \"SMS_SEND_MEMBER_AUDIT\",\"signType\":\"云徙科技\""
		        + ", \"mapValue\": {\"username\": \"" + username + "\",\"reason\": \"" + result + "\",\"other\": \"" + description + "\"}}";

		String string = HttpRequest.sendPost("http://qinqu.dtyunxi.cn/91qinqu-gateway/api/v1/pub/sms/send",p);
		logger.info("【发送入会审核结果短信-入参】：" + p);
		logger.info("【发送入会审核结果短信-出参】：" + string);*/
	}

}
